package org.setamv.springcloud.hystrix.raw;

import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.HystrixCommand;
import org.setamv.springcloud.hystrix.raw.command.FallbackHystrixCommand;
import org.setamv.springcloud.hystrix.raw.command.circuitbreaker.ForceOpenCommand;
import org.setamv.springcloud.hystrix.raw.command.OkHystrixCommand;
import rx.Observable;
import rx.Observer;

/**
 * Hystrix starter.
 */
public class HystrixStarter {

    public static void main(String[] args) {
        // 设置Hystrix的全局配置
        setGlobalConfig();

//        okVisit();
//        okVisitByObserve();
//        okVisitByObservable();
//        badVisit();

        // 测试强制打开断路器的情况
        forceOpenCircuitBreak();
    }

    /**
     * 设置Hystrix的全局配置
     */
    public static void setGlobalConfig() {
        ConfigurationManager.getConfigInstance()
                .setProperty(
                        "hystrix.command.default.execution.isolation.strategy.timeoutInMilliseconds",
                        3000);

    }

    /**
     * 利用Hystrix访问正常的接口，使用execute的方式执行
     */
    public static void okVisit() {
        HystrixCommand<String> command = new OkHystrixCommand();
        String greetings = command.execute();
        System.out.println(greetings);
    }

    /**
     * 测试强制打开断路器的情况
     */
    public static void forceOpenCircuitBreak() {
        HystrixCommand<String> command = new ForceOpenCommand(true);
        String greetings = command.execute();
        System.out.println(greetings);
    }

    /**
     * 利用Hystrix访问不正常的接口（接口要10秒以后才返回）
     */
    public static void badVisit() {
        HystrixCommand<String> command = new FallbackHystrixCommand();
        String greetings = command.execute();
        System.out.println(greetings);
    }

    /**
     * 利用HystrixCommand的Observe方式执行命令
     */
    public static void okVisitByObserve() {
        HystrixCommand<String> command = new OkHystrixCommand();
        // 这里返回一个订阅的主题，并且会立即执行命令
        Observable<String> observable = command.observe();
        // 向主题注册一个订阅器
        observable.subscribe(new Observer<String>() {
            @Override
            public void onCompleted() {
                System.out.println("okVisitByObserve: command execute completed.");
            }

            @Override
            public void onError(Throwable throwable) {
                System.out.println("okVisitByObserve: command execute error.");
            }

            @Override
            public void onNext(String s) {
                System.out.println("okVisitByObserve: result from okVisitByObservable. " + s);
            }
        });
    }

    /**
     * 利用HystrixCommand的Observable方式执行命令
     */
    public static void okVisitByObservable() {
        HystrixCommand<String> command = new OkHystrixCommand();
        // 获取一个可订阅的命令执行的主题，命令并不会立即被执行，而是等到主题被订阅后才开始执行
        Observable<String> observable = command.toObservable();

        // 向主题注册一个订阅器
        observable.subscribe(new Observer<String>() {
            @Override
            public void onCompleted() {
                System.out.println("okVisitByObservable: command execute completed.");
            }

            @Override
            public void onError(Throwable throwable) {
                System.out.println("okVisitByObservable: command execute error.");
            }

            @Override
            public void onNext(String s) {
                System.out.println("okVisitByObservable: result from okVisitByObservable. " + s);
            }
        });
    }
}
